//
//  HWResultsSearchViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/9/3.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWResultsSearchViewController.h"
#import "UIBarButtonItem+Item.h"
#import "HWOnlinegameTableCell.h"
#import "HWGameListModel.h"



@interface HWResultsSearchViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic) UITableView *oneTabview;
@property (strong,nonatomic) NSMutableArray *dataMuArr;

@end

@implementation HWResultsSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //  设置常用的属性
    [self setupAttribute];
    
    //设置UI
    [self setupUI];
    
    if ([self.panduStr isEqualToString:@"1"]) {
        [self listsousu];
        
    } else {
        
        // 搜索的结果
        [self sousuoseachResults];
    }
    
    
    // 搜索的结果
  //  [self sousuoseachResults];
    
    //  传奇
    
    
    
    
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)setupAttribute {
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem  backItemWithimage:[UIImage imageNamed:@"fanhui"] highImage:[UIImage imageNamed:@"fanhui"] target:self action:@selector(back) title:@"   "];
    
    self.navigationItem.title = self.keyword;
    
}

//  设置UI
- (void)setupUI {
    
    self.oneTabview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  self.view.frame.size.height) style:UITableViewStylePlain];
    self.oneTabview.delegate = self;
    self.oneTabview.dataSource = self;
    self.oneTabview.tag = 10;
   //   self.oneTabview.scrollEnabled = NO;
    self.oneTabview.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.oneTabview];
    
}



- (void)back {
    
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}



// 搜索结果
- (void)sousuoseachResults  {
    
    
    NSDictionary *parametes = @{
                                @"ct" : @"app",
                                 @"ios" : @"1",
                                @"ac" : @"search_info",
                                @"keyword" : self.keyword
                                };
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            DLog(@"%@",response);
            self.dataMuArr =   [HWGameListModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
           
        
    
            [self.oneTabview reloadData];
            
        } else {
            
            
            [MBProgressHUD showtext:@"没有搜索数据" icon:nil view:nil];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
    }];
    
    
    
    
    
}

- (void)listsousu {
    
    
    NSDictionary *parametes = @{
                                @"keyword" : self.keyword,
                                @"ct" : @"app",
                                 @"ios" : @"1",
                                @"ac" : @"search_info"
                                };
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            DLog(@"%@",response);
            self.dataMuArr =   [HWGameListModel mj_objectArrayWithKeyValuesArray:response[@"data"]];

            NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
            NSMutableArray *jiluData = [((NSMutableArray *)[defaults objectForKey:@"seachjilu"]) mutableCopy] ;
            [jiluData addObject:self.keyword];
            
            
            NSMutableArray *listAry = [[NSMutableArray alloc]init];
            for (NSString *str in jiluData) {
                if (![listAry containsObject:str]) {
                    [listAry addObject:str];
                }
            }
            
            [defaults setObject:listAry forKey:@"seachjilu"];
            [defaults synchronize];
            [self.oneTabview reloadData];
            
        }else {
            
            
            [MBProgressHUD showtext:@"没有搜索数据" icon:nil view:nil];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
    }];
    
    
    
    
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {



    return  self.dataMuArr.count;

}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *cellID = @"cellID";
        HWOnlinegameTableCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

        if (!cell) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"HWOnlinegameTableCell" owner:nil options:nil].lastObject;
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        HWGameListModel  *model = self.dataMuArr[indexPath.row];
        cell.model = model;
    
        return  cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  75;
    
    
    


}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    HWGameDetailsViewController *gameDetails =[[HWGameDetailsViewController  alloc]init];
     HWGameListModel  *model = self.dataMuArr[indexPath.row];
//    gameDetails.gameId = model.id;
    
//    [self.navigationController pushViewController:gameDetails animated:YES];
    
    HWGameDetailController *detail = [[HWGameDetailController alloc] init];
    detail.gameId = model.id;
    [self.navigationController pushViewController:detail animated:YES];

}

@end
